unit uDMSped;

interface

uses
  SysUtils, Classes, DBClient, Provider, DB, DBTables, ACBrSpedFiscal, inifiles, forms, windows;

type
  TdmSPED = class(TDataModule)
    tqEntidade: TQuery;
    dspEntidade: TDataSetProvider;
    CDSEntidade: TClientDataSet;
    Db: TDatabase;
    CDSEntidadeEMP_RAZAO: TStringField;
    CDSEntidadeEMP_CNPJ: TStringField;
    CDSEntidadeEST_SIGLA: TStringField;
    CDSEntidadeEMP_INSCEST: TStringField;
    CDSEntidadeCID_CODMUNICIPIO: TStringField;
    CDSEntidadeEMP_INSCMUNICIPAL: TStringField;
    CDSEntidadeEMP_IND_ATIV: TStringField;
    CDSEntidadeEMP_FANTASIA: TStringField;
    CDSEntidadeEMP_CEP: TStringField;
    CDSEntidadeEMP_ENDERECO: TStringField;
    CDSEntidadeEMP_NUMERO: TStringField;
    CDSEntidadeEMP_COMPLEMENTO: TStringField;
    CDSEntidadeEMP_BAIRRO: TStringField;
    CDSEntidadeEMP_TELEFONE: TStringField;
    CDSEntidadeEMP_FAX: TStringField;
    CDSEntidadeEMP_EMAIL: TStringField;
    tqUFIEST: TQuery;
    dspUFIEST: TDataSetProvider;
    CDSUFIEST: TClientDataSet;
    dsEntidade: TDataSource;
    CDSEntidadeEMP_CODIGO: TStringField;
    CDSUFIESTEST_SIGLA: TStringField;
    CDSUFIESTEMP_INSCEST_SUBST: TStringField;
    tqContabilista: TQuery;
    dspContabilista: TDataSetProvider;
    CDSContabilista: TClientDataSet;
    CDSContabilistaCONT_NOME: TStringField;
    CDSContabilistaCONT_CPF: TStringField;
    CDSContabilistaCONT_CRC: TStringField;
    CDSContabilistaCONT_CNPJ: TStringField;
    CDSContabilistaCONT_CEP: TStringField;
    CDSContabilistaCONT_ENDERECO: TStringField;
    CDSContabilistaCONT_NUMERO: TStringField;
    CDSContabilistaCONT_COMPLEMENTO: TStringField;
    CDSContabilistaCONT_BAIRRO: TStringField;
    CDSContabilistaCONT_FONE: TStringField;
    CDSContabilistaCONT_FAX: TStringField;
    CDSContabilistaCONT_EMAIL: TStringField;
    CDSContabilistaCID_CODMUNICIPIO: TStringField;
    tqParticipante: TQuery;
    dspParticipante: TDataSetProvider;
    CDSParticipante: TClientDataSet;
    CDSParticipanteENT_CODIGO: TStringField;
    CDSParticipanteENT_NOME: TStringField;
    CDSParticipantePAIS_CODIBGE: TStringField;
    CDSParticipanteENT_CNPJ: TStringField;
    CDSParticipanteENT_CPF: TStringField;
    CDSParticipanteENT_INSCEST: TStringField;
    CDSParticipanteCID_CODMUNICIPIO: TStringField;
    CDSParticipanteCOD_SUFRAMA: TStringField;
    CDSParticipanteEND_ENDERECO: TStringField;
    CDSParticipanteEND_NUMERO: TStringField;
    CDSParticipanteEND_BAIRRO: TStringField;
    tqAltParticipante: TQuery;
    dspAltParticipante: TDataSetProvider;
    CDSAltParticipante: TClientDataSet;
    dsParticipante: TDataSource;
    tqUnidade: TQuery;
    dspUnidade: TDataSetProvider;
    CDSUnidade: TClientDataSet;
    CDSUnidadeUND_CODIGO: TStringField;
    CDSUnidadeUND_DESCRICAO: TStringField;
    tqItem: TQuery;
    dspItem: TDataSetProvider;
    CDSItem: TClientDataSet;
    dsItem: TDataSource;
    tqAltItem: TQuery;
    dspAltItem: TDataSetProvider;
    CDSAltItem: TClientDataSet;
    CDSAltItemDATA_HORA: TDateTimeField;
    CDSAltItemOPERACAO: TStringField;
    CDSAltItemPRO_CODIGO_V: TStringField;
    CDSAltItemPRO_CODIGO_N: TStringField;
    CDSAltItemPRO_NOME_V: TStringField;
    CDSAltItemPRO_NOME_N: TStringField;
    tqFator: TQuery;
    dspFator: TDataSetProvider;
    CDSFator: TClientDataSet;
    DateTimeField1: TDateTimeField;
    StringField1: TStringField;
    StringField2: TStringField;
    StringField3: TStringField;
    StringField4: TStringField;
    StringField5: TStringField;
    tqBem: TQuery;
    dspBem: TDataSetProvider;
    CDSBem: TClientDataSet;
    CDSBemCOD_IND_BEM: TStringField;
    CDSBemIDENT_MERC: TStringField;
    CDSBemDESCR_ITEM: TMemoField;
    CDSBemCOD_PRNC: TStringField;
    CDSBemHIST_CODIGO: TStringField;
    CDSBemNR_PARC: TFloatField;
    CDSBemDEP_CODIGO: TStringField;
    CDSBemFUNC: TMemoField;
    CDSBemVIDA_UTIL: TFloatField;
    SPED: TACBrSPEDFiscal;
    CDSNatOper: TClientDataSet;
    dspNatOper: TDataSetProvider;
    tqNatOper: TQuery;
    tqInfCompl: TQuery;
    dspInfCompl: TDataSetProvider;
    CDSInfCompl: TClientDataSet;
    CDSInfComplCOD_MENSAGEM: TStringField;
    CDSInfComplTEXTO: TBlobField;
    tqObservacoes: TQuery;
    dspObservacoes: TDataSetProvider;
    CDSObservacoes: TClientDataSet;
    CDSObservacoesCOD_OBS: TStringField;
    CDSObservacoesTXT: TBlobField;
    tqPlanoContas: TQuery;
    dspPlanoContas: TDataSetProvider;
    CDSPlanoContas: TClientDataSet;
    CDSPlanoContasCOD_CTA: TStringField;
    CDSPlanoContasDT_ALT: TDateTimeField;
    CDSPlanoContasCOD_NAT_CC: TStringField;
    CDSPlanoContasIND_CTA: TStringField;
    CDSPlanoContasNIVEL: TFloatField;
    CDSPlanoContasNOME_CTA: TStringField;
    tqDepartamento: TQuery;
    dspDepartamento: TDataSetProvider;
    CDSDepartamento: TClientDataSet;
    CDSDepartamentoDEP_CODIGO: TStringField;
    CDSDepartamentoDEP_DESCRICAO: TStringField;
    CDSDepartamentoVL_HORAH: TFloatField;
    CDSDepartamentoVL_HORAM: TFloatField;
    CDSDepartamentoDATA_ATUAL: TDateTimeField;
    CDSDepartamentoRESPONSAVEL: TStringField;
    CDSDepartamentoDEP_ORDEM: TFloatField;
    CDSDepartamentoDEP_ABREVIACAO: TStringField;
    tqNotaFiscalV: TQuery;
    dspNotaFiscalV: TDataSetProvider;
    CDSNotaFiscalV: TClientDataSet;
    dsNotaFiscalV: TDataSource;
    tqNotaFiscalC: TQuery;
    dspNotaFiscalC: TDataSetProvider;
    CDSNotaFiscalC: TClientDataSet;
    dsNotaFiscalC: TDataSource;
    tqItemNotaFiscalV: TQuery;
    dspItemNotaFiscalV: TDataSetProvider;
    CDSItemNotaFiscalV: TClientDataSet;
    tqC110: TQuery;
    dspC110: TDataSetProvider;
    CDSC110: TClientDataSet;
    CDSC110COD_MENSAGEM: TStringField;
    CDSC110TEXTO: TBlobField;
    tqFatura: TQuery;
    dspFatura: TDataSetProvider;
    CDSFatura: TClientDataSet;
    CDSFaturaTITC_NUMERO: TStringField;
    CDSFaturaTITC_DATAVENC: TDateTimeField;
    CDSFaturaTITC_VALOR: TFloatField;
    CDSItemNotaFiscalVITNV_ITEM: TStringField;
    CDSItemNotaFiscalVPRO_CODIGO: TStringField;
    CDSItemNotaFiscalVDESCR_COMPL: TStringField;
    CDSItemNotaFiscalVITNV_QUANTIDADE: TFloatField;
    CDSItemNotaFiscalVPRO_UNIDADE: TStringField;
    CDSItemNotaFiscalVVL_ITEM: TFloatField;
    CDSItemNotaFiscalVITNV_DESCONTOITEM: TFloatField;
    CDSItemNotaFiscalVIND_MOV: TStringField;
    CDSItemNotaFiscalVITNV_CST: TStringField;
    CDSItemNotaFiscalVCODF_CODIGO: TStringField;
    CDSItemNotaFiscalVID_OPERACAO: TStringField;
    CDSItemNotaFiscalVITNV_BCALCICMS: TFloatField;
    CDSItemNotaFiscalVITNV_ALIQICMS: TFloatField;
    CDSItemNotaFiscalVITNV_ICMS: TFloatField;
    CDSItemNotaFiscalVITNV_BCALCICMSSUBS: TFloatField;
    CDSItemNotaFiscalVITNV_ALIQICMSSUBS: TFloatField;
    CDSItemNotaFiscalVITNV_ICMSSUBS: TFloatField;
    CDSItemNotaFiscalVITNV_CST_IPI: TStringField;
    CDSItemNotaFiscalVITNV_COD_ENQ: TStringField;
    CDSItemNotaFiscalVITNV_BCALCIPI: TFloatField;
    CDSItemNotaFiscalVITNV_ALIQIPI: TFloatField;
    CDSItemNotaFiscalVITNV_IPI: TFloatField;
    CDSItemNotaFiscalVITNV_CST_PIS: TStringField;
    CDSItemNotaFiscalVITNV_ALIQPIS_P: TFloatField;
    CDSItemNotaFiscalVITNV_QT_BCALCPIS: TFloatField;
    CDSItemNotaFiscalVITNV_ALIQPIS_R: TFloatField;
    CDSItemNotaFiscalVITNV_PIS: TFloatField;
    CDSItemNotaFiscalVITNV_CST_COFINS: TStringField;
    CDSItemNotaFiscalVITNV_BCALCCOFINS: TFloatField;
    CDSItemNotaFiscalVITNV_ALIQCOFINS_P: TFloatField;
    CDSItemNotaFiscalVITNV_QT_BCALCCOFINS: TFloatField;
    CDSItemNotaFiscalVITNV_ALIQCOFINS_R: TFloatField;
    CDSItemNotaFiscalVITNV_COFINS: TFloatField;
    CDSItemNotaFiscalVHIST_CONTABIL: TStringField;
    CDSItemNotaFiscalVITNV_BCALCPIS: TFloatField;
    tqC190V: TQuery;
    dspC190V: TDataSetProvider;
    CDSC190V: TClientDataSet;
    tqC195: TQuery;
    dspC195: TDataSetProvider;
    CDSC195: TClientDataSet;
    CDSC195COD_OBS: TStringField;
    CDSC195TXT: TBlobField;
    tqItemNotaFiscalC: TQuery;
    dspItemNotaFiscalC: TDataSetProvider;
    CDSItemNotaFiscalC: TClientDataSet;
    tqC190C: TQuery;
    dspC190C: TDataSetProvider;
    CDSC190C: TClientDataSet;
    CDSC190CITNC_CST: TStringField;
    CDSC190CCODF_CODIGO: TStringField;
    CDSC190CITNC_ALIQICMS: TFloatField;
    CDSC190CVL_OPR: TFloatField;
    CDSC190CVL_BC_ICMS: TFloatField;
    CDSC190CVL_ICMS: TFloatField;
    CDSC190CVL_BC_ICMS_ST: TFloatField;
    CDSC190CVL_ICMS_ST: TFloatField;
    CDSC190CVL_RED_BC: TFloatField;
    CDSC190CVL_IPI: TFloatField;
    CDSC190VITNV_CST: TStringField;
    CDSC190VCODF_CODIGO: TStringField;
    CDSC190VITNV_ALIQICMS: TFloatField;
    CDSC190VVL_OPR: TFloatField;
    CDSC190VVL_BC_ICMS: TFloatField;
    CDSC190VVL_ICMS: TFloatField;
    CDSC190VVL_BC_ICMS_ST: TFloatField;
    CDSC190VVL_ICMS_ST: TFloatField;
    CDSC190VVL_RED_BC: TFloatField;
    CDSC190VVL_IPI: TFloatField;
    tqC113: TQuery;
    dspC113: TDataSetProvider;
    CDSC113: TClientDataSet;
    CDSC113ID: TFloatField;
    CDSC113NOTA_SERIE: TStringField;
    CDSC113NOTA_NUMERO: TStringField;
    CDSC113NOTC_SERIE: TStringField;
    CDSC113NOTAC_NUMERO: TStringField;
    CDSC113ENT_CODIGO: TStringField;
    CDSC113EMP_CODIGO: TStringField;
    CDSC113CUF: TFloatField;
    CDSC113DATA_DOC: TFloatField;
    CDSC113CNPJ: TStringField;
    CDSC113MOD: TFloatField;
    CDSC113SERIE: TFloatField;
    CDSC113NNF: TFloatField;
    CDSAltParticipanteDATA_HORA: TDateTimeField;
    CDSAltParticipanteNR_CAMPO: TStringField;
    CDSAltParticipanteCONT_ANT: TStringField;
    CDSAltParticipanteENT_CODIGO_V: TStringField;
    CDSFaturaNUMERO: TFloatField;
    CDSItemNotaFiscalVNOTA_SERIE: TStringField;
    CDSItemNotaFiscalVNOTA_NUMERO: TStringField;
    CDSItemNotaFiscalVENT_CODIGO: TStringField;
    CDSNotaFiscalVIND_EMIT: TStringField;
    CDSNotaFiscalVNOTA_SERIE: TStringField;
    CDSNotaFiscalVNOTA_NUMERO: TStringField;
    CDSNotaFiscalVBKP_SERIE: TStringField;
    CDSNotaFiscalVBKP_NUMERO: TStringField;
    CDSNotaFiscalVENT_CODIGO: TStringField;
    CDSNotaFiscalVTRAN_ENTIDADE: TStringField;
    CDSNotaFiscalVMOD_CODIGO: TStringField;
    CDSNotaFiscalVCOD_SIT: TStringField;
    CDSNotaFiscalVNFE_CHAVE: TStringField;
    CDSNotaFiscalVNOTV_DATAFAT: TDateTimeField;
    CDSNotaFiscalVNOTV_DATASAIDA: TDateTimeField;
    CDSNotaFiscalVNOTV_VALORCONTABIL: TFloatField;
    CDSNotaFiscalVIND_PGTO: TStringField;
    CDSNotaFiscalVNOTV_TOTALDESCONTO: TFloatField;
    CDSNotaFiscalVVL_ABAT_NT: TFloatField;
    CDSNotaFiscalVNOTV_TOTALPRODUTO: TFloatField;
    CDSNotaFiscalVIND_FRT: TStringField;
    CDSNotaFiscalVNOTV_FRETE: TFloatField;
    CDSNotaFiscalVNOTV_SEGURO: TFloatField;
    CDSNotaFiscalVNOTV_EMBALAGEM: TFloatField;
    CDSNotaFiscalVNOTV_BCALICMS: TFloatField;
    CDSNotaFiscalVNOTV_ICMS: TFloatField;
    CDSNotaFiscalVNOTV_BCALICMSUBS: TFloatField;
    CDSNotaFiscalVNOTV_ICMSUBS: TFloatField;
    CDSNotaFiscalVNOTV_IPI: TFloatField;
    CDSNotaFiscalVNOTV_PIS: TFloatField;
    CDSNotaFiscalVNOTV_COFINS: TFloatField;
    CDSNotaFiscalVNOTV_PIS_ST: TFloatField;
    CDSNotaFiscalVNOTV_COFINS_ST: TFloatField;
    CDSNotaFiscalVCOD_MENSAGEM: TStringField;
    CDSNotaFiscalVNOTV_MENSAGEM: TBlobField;
    CDSNotaFiscalVCOD_OBS: TStringField;
    CDSNotaFiscalVNOTV_OBS_LANCFISCAL: TMemoField;
    CDSNotaFiscalVQTD_VOL: TFloatField;
    CDSNotaFiscalVNOTV_PESOBRU: TFloatField;
    CDSNotaFiscalVNOTV_PESOLIQ: TFloatField;
    CDSNotaFiscalVUF_ID: TStringField;
    CDSNotaFiscalVVEIC_ID: TStringField;
    CDSNotaFiscalVQTD_PARC: TFloatField;
    CDSNotaFiscalVVL_TIT: TFloatField;
    tqItemNotaFiscalVNOTA_SERIE: TStringField;
    tqItemNotaFiscalVNOTA_NUMERO: TStringField;
    tqItemNotaFiscalVENT_CODIGO: TStringField;
    tqItemNotaFiscalVITNV_ITEM: TStringField;
    tqItemNotaFiscalVPRO_CODIGO: TStringField;
    tqItemNotaFiscalVDESCR_COMPL: TStringField;
    tqItemNotaFiscalVITNV_QUANTIDADE: TFloatField;
    tqItemNotaFiscalVPRO_UNIDADE: TStringField;
    tqItemNotaFiscalVVL_ITEM: TFloatField;
    tqItemNotaFiscalVITNV_DESCONTOITEM: TFloatField;
    tqItemNotaFiscalVIND_MOV: TStringField;
    tqItemNotaFiscalVITNV_CST: TStringField;
    tqItemNotaFiscalVCODF_CODIGO: TStringField;
    tqItemNotaFiscalVID_OPERACAO: TStringField;
    tqItemNotaFiscalVITNV_BCALCICMS: TFloatField;
    tqItemNotaFiscalVITNV_ALIQICMS: TFloatField;
    tqItemNotaFiscalVITNV_ICMS: TFloatField;
    tqItemNotaFiscalVITNV_BCALCICMSSUBS: TFloatField;
    tqItemNotaFiscalVITNV_ALIQICMSSUBS: TFloatField;
    tqItemNotaFiscalVITNV_ICMSSUBS: TFloatField;
    tqItemNotaFiscalVITNV_CST_IPI: TStringField;
    tqItemNotaFiscalVITNV_COD_ENQ: TStringField;
    tqItemNotaFiscalVITNV_BCALCIPI: TFloatField;
    tqItemNotaFiscalVITNV_ALIQIPI: TFloatField;
    tqItemNotaFiscalVITNV_IPI: TFloatField;
    tqItemNotaFiscalVITNV_CST_PIS: TStringField;
    tqItemNotaFiscalVITNV_BCALCPIS: TFloatField;
    tqItemNotaFiscalVITNV_ALIQPIS_P: TFloatField;
    tqItemNotaFiscalVITNV_QT_BCALCPIS: TFloatField;
    tqItemNotaFiscalVITNV_ALIQPIS_R: TFloatField;
    tqItemNotaFiscalVITNV_PIS: TFloatField;
    tqItemNotaFiscalVITNV_CST_COFINS: TStringField;
    tqItemNotaFiscalVITNV_BCALCCOFINS: TFloatField;
    tqItemNotaFiscalVITNV_ALIQCOFINS_P: TFloatField;
    tqItemNotaFiscalVITNV_QT_BCALCCOFINS: TFloatField;
    tqItemNotaFiscalVITNV_ALIQCOFINS_R: TFloatField;
    tqItemNotaFiscalVITNV_COFINS: TFloatField;
    tqItemNotaFiscalVHIST_CONTABIL: TStringField;
    CDSUnidadePRO_CODIGO: TStringField;
    CDSItemNotaFiscalCNOTC_SERIE: TStringField;
    CDSItemNotaFiscalCNOTAC_NUMERO: TStringField;
    CDSItemNotaFiscalCENT_CODIGO: TStringField;
    CDSItemNotaFiscalCNOTC_ITEM: TStringField;
    CDSItemNotaFiscalCPRO_CODIGO: TStringField;
    CDSItemNotaFiscalCDESCR_COMPL: TStringField;
    CDSItemNotaFiscalCITNC_QUANTIDADE: TFloatField;
    CDSItemNotaFiscalCPRO_UNIDADE: TStringField;
    CDSItemNotaFiscalCVL_ITEM: TFloatField;
    CDSItemNotaFiscalCIND_MOV: TStringField;
    CDSItemNotaFiscalCITNC_CST: TStringField;
    CDSItemNotaFiscalCCODF_CODIGO: TStringField;
    CDSItemNotaFiscalCID_OPERACAO: TStringField;
    CDSItemNotaFiscalCITNC_BCALCICMS: TFloatField;
    CDSItemNotaFiscalCITNC_ALIQICMS: TFloatField;
    CDSItemNotaFiscalCITNC_ICMS: TFloatField;
    CDSItemNotaFiscalCITNC_BCALCICMSSUBS: TFloatField;
    CDSItemNotaFiscalCITNC_ALIQICMSSUBS: TFloatField;
    CDSItemNotaFiscalCITNC_ICMSSUBS: TFloatField;
    CDSItemNotaFiscalCITNC_CST_IPI: TStringField;
    CDSItemNotaFiscalCITNC_COD_ENQ: TStringField;
    CDSItemNotaFiscalCITNC_BCALCIPI: TFloatField;
    CDSItemNotaFiscalCITNC_ALIQIPI: TFloatField;
    CDSItemNotaFiscalCITNC_IPI: TFloatField;
    CDSItemNotaFiscalCITNC_CST_PIS: TStringField;
    CDSItemNotaFiscalCITNC_BCALCPIS: TFloatField;
    CDSItemNotaFiscalCITNC_ALIQPIS_P: TFloatField;
    CDSItemNotaFiscalCITNC_QT_BCALCPIS: TFloatField;
    CDSItemNotaFiscalCITNC_ALIQPIS_R: TFloatField;
    CDSItemNotaFiscalCITNC_PIS: TFloatField;
    CDSItemNotaFiscalCITNC_CST_COFINS: TStringField;
    CDSItemNotaFiscalCITNC_BCALCCOFINS: TFloatField;
    CDSItemNotaFiscalCITNC_ALIQCOFINS_P: TFloatField;
    CDSItemNotaFiscalCITNC_QT_BCALCCOFINS: TFloatField;
    CDSItemNotaFiscalCITNC_ALIQCOFINS_R: TFloatField;
    CDSItemNotaFiscalCITNC_COFINS: TFloatField;
    CDSItemNotaFiscalCHIST_CONTABIL: TStringField;
    CDSFaturaENT_CODIGO: TStringField;
    CDSFaturaNOTA_NUMERO: TStringField;
    CDSFaturaNOTAC_NUMERO: TStringField;
    CDSFaturaNOTA_SERIE: TStringField;
    CDSFaturaNOTC_SERIE: TStringField;
    CDSNatOperNATO_CODIGO: TStringField;
    CDSNatOperNATO_DESCRICAO: TStringField;
    E510: TClientDataSet;
    E510CFOP: TStringField;
    E510CST_IPI: TStringField;
    E510VL_CONT_IPI: TFloatField;
    E510VL_BC_IPI: TFloatField;
    E510VL_IPI: TFloatField;
    CDSItemPRO_CODIGO: TStringField;
    CDSItemPRO_NOME: TStringField;
    CDSItemPRO_EAN: TStringField;
    CDSItemPRO_UNIDADE: TStringField;
    CDSItemFASE_CODIGO: TStringField;
    CDSItemCOD_NCM: TStringField;
    CDSItemPRO_ALIQICMS: TFloatField;
    tqNotaFiscalVIND_EMIT: TStringField;
    tqNotaFiscalVNOTA_SERIE: TStringField;
    tqNotaFiscalVNOTA_NUMERO: TStringField;
    tqNotaFiscalVBKP_SERIE: TStringField;
    tqNotaFiscalVBKP_NUMERO: TStringField;
    tqNotaFiscalVENT_CODIGO: TStringField;
    tqNotaFiscalVTRAN_ENTIDADE: TStringField;
    tqNotaFiscalVMOD_CODIGO: TStringField;
    tqNotaFiscalVCOD_SIT: TStringField;
    tqNotaFiscalVNFE_CHAVE: TStringField;
    tqNotaFiscalVNOTV_DATAFAT: TDateTimeField;
    tqNotaFiscalVNOTV_DATASAIDA: TDateTimeField;
    tqNotaFiscalVNOTV_VALORCONTABIL: TFloatField;
    tqNotaFiscalVIND_PGTO: TStringField;
    tqNotaFiscalVNOTV_TOTALDESCONTO: TFloatField;
    tqNotaFiscalVVL_ABAT_NT: TFloatField;
    tqNotaFiscalVNOTV_TOTALPRODUTO: TFloatField;
    tqNotaFiscalVIND_FRT: TStringField;
    tqNotaFiscalVNOTV_FRETE: TFloatField;
    tqNotaFiscalVNOTV_SEGURO: TFloatField;
    tqNotaFiscalVNOTV_EMBALAGEM: TFloatField;
    tqNotaFiscalVNOTV_BCALICMS: TFloatField;
    tqNotaFiscalVNOTV_ICMS: TFloatField;
    tqNotaFiscalVNOTV_BCALICMSUBS: TFloatField;
    tqNotaFiscalVNOTV_ICMSUBS: TFloatField;
    tqNotaFiscalVNOTV_IPI: TFloatField;
    tqNotaFiscalVNOTV_PIS: TFloatField;
    tqNotaFiscalVNOTV_COFINS: TFloatField;
    tqNotaFiscalVNOTV_PIS_ST: TFloatField;
    tqNotaFiscalVNOTV_COFINS_ST: TFloatField;
    tqNotaFiscalVCOD_MENSAGEM: TStringField;
    tqNotaFiscalVNOTV_MENSAGEM: TBlobField;
    tqNotaFiscalVCOD_OBS: TStringField;
    tqNotaFiscalVNOTV_OBS_LANCFISCAL: TMemoField;
    tqNotaFiscalVQTD_VOL: TFloatField;
    tqNotaFiscalVNOTV_PESOBRU: TFloatField;
    tqNotaFiscalVNOTV_PESOLIQ: TFloatField;
    tqNotaFiscalVUF_ID: TStringField;
    tqNotaFiscalVVEIC_ID: TStringField;
    tqNotaFiscalVQTD_PARC: TFloatField;
    tqNotaFiscalVVL_TIT: TFloatField;
    tqNotaFiscalVREF_NUMERO: TStringField;
    CDSNotaFiscalVREF_NUMERO: TStringField;
    tqNotaFiscalCIND_EMIT: TStringField;
    tqNotaFiscalCNOTA_SERIE: TStringField;
    tqNotaFiscalCNOTA_NUMERO: TStringField;
    tqNotaFiscalCBKP_SERIE: TStringField;
    tqNotaFiscalCBKP_NOTA: TStringField;
    tqNotaFiscalCENT_CODIGO: TStringField;
    tqNotaFiscalCTRAN_ENTIDADE: TStringField;
    tqNotaFiscalCMOD_CODIGO: TStringField;
    tqNotaFiscalCCOD_SIT: TStringField;
    tqNotaFiscalCNFCE_CHAVE: TStringField;
    tqNotaFiscalCNOTC_DATAFATURA: TDateTimeField;
    tqNotaFiscalCNOTC_DATAENTRADA: TDateTimeField;
    tqNotaFiscalCNOTC_VALORCONTABIL: TFloatField;
    tqNotaFiscalCIND_PGTO: TStringField;
    tqNotaFiscalCNOTC_DESCONTO: TFloatField;
    tqNotaFiscalCVL_ABAT_NT: TFloatField;
    tqNotaFiscalCNOTC_TOTALPRODUTO: TFloatField;
    tqNotaFiscalCIND_FRT: TStringField;
    tqNotaFiscalCNOTC_VALORFRETE: TFloatField;
    tqNotaFiscalCNOTC_VALORSEGURO: TFloatField;
    tqNotaFiscalCNOTC_DESP_ACESSORIA: TFloatField;
    tqNotaFiscalCNOTC_BCALCICMS: TFloatField;
    tqNotaFiscalCNOTC_VALORICMS: TFloatField;
    tqNotaFiscalCNOTC_BCALCICMSSUBS: TFloatField;
    tqNotaFiscalCNOTC_VALORICMSSUBS: TFloatField;
    tqNotaFiscalCNOTC_VALORIPI: TFloatField;
    tqNotaFiscalCNOTC_PIS: TFloatField;
    tqNotaFiscalCNOTC_COFINS: TFloatField;
    tqNotaFiscalCNOTC_PIS_ST: TFloatField;
    tqNotaFiscalCNOTC_COFINS_ST: TFloatField;
    tqNotaFiscalCCOD_MENSAGEM: TStringField;
    tqNotaFiscalCNOTC_MENSAGEM: TBlobField;
    tqNotaFiscalCCOD_OBS: TStringField;
    tqNotaFiscalCNOTC_OBS_LANCFISCAL: TMemoField;
    tqNotaFiscalCQTD_VOL: TFloatField;
    tqNotaFiscalCNOTV_PESOBRU: TFloatField;
    tqNotaFiscalCNOTV_PESOLIQ: TFloatField;
    tqNotaFiscalCUF_ID: TStringField;
    tqNotaFiscalCVEIC_ID: TStringField;
    tqNotaFiscalCQTD_PARC: TFloatField;
    tqNotaFiscalCVL_TIT: TFloatField;
    CDSNotaFiscalCIND_EMIT: TStringField;
    CDSNotaFiscalCNOTA_SERIE: TStringField;
    CDSNotaFiscalCNOTA_NUMERO: TStringField;
    CDSNotaFiscalCBKP_SERIE: TStringField;
    CDSNotaFiscalCBKP_NOTA: TStringField;
    CDSNotaFiscalCENT_CODIGO: TStringField;
    CDSNotaFiscalCTRAN_ENTIDADE: TStringField;
    CDSNotaFiscalCMOD_CODIGO: TStringField;
    CDSNotaFiscalCCOD_SIT: TStringField;
    CDSNotaFiscalCNFCE_CHAVE: TStringField;
    CDSNotaFiscalCNOTC_DATAFATURA: TDateTimeField;
    CDSNotaFiscalCNOTC_DATAENTRADA: TDateTimeField;
    CDSNotaFiscalCNOTC_VALORCONTABIL: TFloatField;
    CDSNotaFiscalCIND_PGTO: TStringField;
    CDSNotaFiscalCNOTC_DESCONTO: TFloatField;
    CDSNotaFiscalCVL_ABAT_NT: TFloatField;
    CDSNotaFiscalCNOTC_TOTALPRODUTO: TFloatField;
    CDSNotaFiscalCIND_FRT: TStringField;
    CDSNotaFiscalCNOTC_VALORFRETE: TFloatField;
    CDSNotaFiscalCNOTC_VALORSEGURO: TFloatField;
    CDSNotaFiscalCNOTC_DESP_ACESSORIA: TFloatField;
    CDSNotaFiscalCNOTC_BCALCICMS: TFloatField;
    CDSNotaFiscalCNOTC_VALORICMS: TFloatField;
    CDSNotaFiscalCNOTC_BCALCICMSSUBS: TFloatField;
    CDSNotaFiscalCNOTC_VALORICMSSUBS: TFloatField;
    CDSNotaFiscalCNOTC_VALORIPI: TFloatField;
    CDSNotaFiscalCNOTC_PIS: TFloatField;
    CDSNotaFiscalCNOTC_COFINS: TFloatField;
    CDSNotaFiscalCNOTC_PIS_ST: TFloatField;
    CDSNotaFiscalCNOTC_COFINS_ST: TFloatField;
    CDSNotaFiscalCCOD_MENSAGEM: TStringField;
    CDSNotaFiscalCNOTC_MENSAGEM: TBlobField;
    CDSNotaFiscalCCOD_OBS: TStringField;
    CDSNotaFiscalCNOTC_OBS_LANCFISCAL: TMemoField;
    CDSNotaFiscalCQTD_VOL: TFloatField;
    CDSNotaFiscalCNOTV_PESOBRU: TFloatField;
    CDSNotaFiscalCNOTV_PESOLIQ: TFloatField;
    CDSNotaFiscalCUF_ID: TStringField;
    CDSNotaFiscalCVEIC_ID: TStringField;
    CDSNotaFiscalCQTD_PARC: TFloatField;
    CDSNotaFiscalCVL_TIT: TFloatField;
    E110: TClientDataSet;
    E110VL_TOT_DEBITOS: TFloatField;
    E110VL_TOT_CREDITOS: TFloatField;
    tqNotaFiscalCCODF_CODIGO: TStringField;
    CDSNotaFiscalCCODF_CODIGO: TStringField;
    tqNotaFiscalVCODF_CODIGO: TStringField;
    CDSNotaFiscalVCODF_CODIGO: TStringField;
    D100: TClientDataSet;
    D100COD_PART: TStringField;
    D100COD_MOD: TStringField;
    D100COD_SIT: TStringField;
    D100SER: TStringField;
    D100NUM_DOC: TStringField;
    D100CHV_CTE: TStringField;
    D100DT_A_P: TDateField;
    D100VL_DOC: TFloatField;
    D100VL_DESC: TFloatField;
    D100VL_SERV: TFloatField;
    D100VL_BC_ICMS: TFloatField;
    D100VL_ICMS: TFloatField;
    D100VL_NT: TFloatField;
    D100COD_CTA: TStringField;
    D100IND_FRT: TIntegerField;
    D100DT_DOC: TStringField;
    D100CFOP: TStringField;
    D100ALIQ: TFloatField;
    D100IND_EMIT: TIntegerField;
    CDSItemNotaFiscalCREF_NOTAC_NUMERO: TStringField;
    CDSItemNotaFiscalCITNC_DESCONTO: TFloatField;
    procedure CDSAltParticipanteAfterOpen(DataSet: TDataSet);
    procedure CDSAltItemAfterOpen(DataSet: TDataSet);
    procedure DataModuleCreate(Sender: TObject);
    procedure DataModuleDestroy(Sender: TObject);
  private
    { Private declarations }
    arq: string;
    procedure importaCTRC;
    function recuperaEntidade(pCnpjCPF: string): string;
    function tamanho(campo: string): Integer;
    function posicao(campo: string): Integer;
  public
    { Public declarations }
    AstrEntidades: string;
    function tira_pt(p_frase: string): string;
  end;

var
  dmSPED: TdmSPED;

implementation

{$R *.dfm}

function LerArquivoIni(AArquivo, ATopico, ASubTopico: string): string;
var
  ArqINI: TINIFile;
  Valor: string;
begin
  if not FileExists(AArquivo) then
    result := ''
  else begin
    try
      ArqINI := TIniFile.Create(AArquivo);
      if Trim(ASubTopico) <> '' then
        Valor := ArqIni.ReadString(Atopico, ASubTopico, '')
      else
        Valor := '';
      Result := Valor;
      FreeAndNil(ArqIni);
    except
      Result := '';
    end;

  end;
end;

procedure DirList(ASource: string; ADirList: TStringList);
var
  SearchRec: TSearchRec;
  Result: integer;
begin
  ADirList.Clear;
  Result := FindFirst(ASource, faAnyFile, SearchRec);
  if Result = 0 then
    while (Result = 0) do begin
      if (SearchRec.Name + ' ')[1] = '.' then begin { Se pegou nome de SubDiretorio }
        Result := FindNext(SearchRec);
        Continue;
      end;
      ADirList.Add(SearchRec.Name);
      Result := FindNext(SearchRec);
    end;
  //FindClose(SearchRec);
  ADirList.Sort;
end;

procedure TdmSPED.CDSAltParticipanteAfterOpen(DataSet: TDataSet);
begin
  CDSAltParticipante.Filtered := False;
  CDSAltParticipante.Filter := 'ENT_CODIGO_V = ' + QuotedStr(CDSParticipanteENT_CODIGO.AsString);
  CDSAltParticipante.Filtered := True;
end;

procedure TdmSPED.CDSAltItemAfterOpen(DataSet: TDataSet);
begin
  CDSAltItem.Filtered := False;
  CDSAltItem.Filter := 'PRO_CODIGO_V = ' + QuotedStr(CDSItemPRO_CODIGO.AsString);
  CDSAltItem.Filtered := True;
end;

procedure TdmSPED.DataModuleCreate(Sender: TObject);
begin
  db.Connected := True;
  importaCTRC;
end;

procedure TdmSPED.DataModuleDestroy(Sender: TObject);
begin
  Db.Connected := False;
end;

function TdmSPED.tira_pt(p_frase: string): string;
var
  cont: Integer;
  c: string;
  d: string;
begin
  d := '';
  for cont := 1 to length(p_frase) do begin
    c := Copy(p_frase, cont, 1);
    if (c <> chr(39)) and (c <> ':') and (c <> '.') and (c <> '-') and (c <> ',') and (c <> ' ') and (c <> '\') and (c <> '/') and (c <> '_') then begin
      d := d + c;
    end;
  end;
  result := d;
end;

procedure TdmSPED.importaCTRC;
var
  lista: TStringlist;
  CTRC: TStringlist;
  c, i: Integer;
begin
  AstrEntidades := '';
  arq := ExtractFilePath(Application.ExeName) + 'CT.ini';
  try
    lista := TStringlist.create;
    DirList(LerArquivoIni(arq, 'DIRETORIO', 'NOME') + '*.txt', lista);
    Lista.Sort;

    D100.Close;
    D100.CreateDataSet;
    D100.Open;

    for i := 0 to lista.Count - 1 do begin
      try
        CTRC := TStringlist.Create;
        CTRC.LoadFromFile(LerArquivoIni(arq, 'DIRETORIO', 'NOME') + lista.Strings[i]);
        for c := 0 to CTRC.Count - 1 do begin
          if trim(CTRC.Strings[c]) <> '' then begin
            D100.Append;
            D100COD_PART.AsString := recuperaEntidade(copy(CTRC.Strings[c], posicao('COD_PART'), tamanho('COD_PART')));
            D100COD_MOD.AsString := copy(CTRC.Strings[c], posicao('COD_MOD'), tamanho('COD_MOD'));
            D100SER.AsString := copy(CTRC.Strings[c], posicao('SER'), tamanho('SER'));
            D100NUM_DOC.AsString := Trim(copy(CTRC.Strings[c], posicao('NUM_DOCI'), tamanho('NUM_DOCI')));
            D100DT_DOC.AsString := copy(CTRC.Strings[c], posicao('DT_DOC'), tamanho('DT_DOC'));
            D100VL_DOC.AsFloat := StrToFloatDef(copy(CTRC.Strings[c], posicao('VALORCONTABIL'), tamanho('VALORCONTABIL')), 0) / 100;
            if copy(CTRC.Strings[c], posicao('COD_OBS'), tamanho('COD_OBS')) = 'CIF' then
              D100IND_FRT.AsInteger := 1
            else
              D100IND_FRT.AsInteger := 2;
            if copy(CTRC.Strings[c], 72, 10) = '1111111111' then
              D100COD_SIT.AsInteger := 2
            else
              D100COD_SIT.AsInteger := 0;
            D100IND_EMIT.AsInteger := 0;
            D100VL_BC_ICMS.AsFloat := StrToFloatDef(copy(CTRC.Strings[c], posicao('BCALCICMS'), tamanho('BCALCICMS')), 0) / 100;
            D100VL_ICMS.AsFloat := StrToFloatDef(copy(CTRC.Strings[c], posicao('IMPOSTOICMS'), tamanho('IMPOSTOICMS')), 0) / 100;
            D100VL_NT.AsFloat := StrToFloatDef(copy(CTRC.Strings[c], posicao('ISENTASICMS'), tamanho('ISENTASICMS')), 0) / 100;
            D100ALIQ.AsFloat := StrToFloatDef(copy(CTRC.Strings[c], posicao('ALIQ'), tamanho('ALIQ')), 0) / 100;
            D100CFOP.AsString := copy(CTRC.Strings[c], posicao('CFOP'), tamanho('CFOP'));
            D100.Post;
          end;
        end;
      finally
        CTRC.Free;
      end;
    end;

    if AstrEntidades <> '' then
      tqParticipante.SQL.Add('or (e.Ent_codigo in (' + AstrEntidades + ') ) )')
    else
      tqParticipante.SQL.Add(')');
  finally
    lista.Free;
  end;
end;

function TdmSPED.recuperaEntidade(pCnpjCPF: string): string;
var
  tq: TQuery;
begin
  tq := TQuery.Create(Self);
  tq.DatabaseName := Db.DatabaseName;
  tq.Close;
  tq.SQL.Clear;
  tq.SQL.Add('Select Ent_codigo from Entidade where Ent_CNPJ = ' + QuotedStr(pCnpjCPF) + ' or Ent_CPF = ' + QuotedStr(pCnpjCPF));
  tq.Open;
  if not tq.Eof then begin
    Result := tq.FieldByName('Ent_codigo').AsString;
    if AstrEntidades = '' then
      AstrEntidades := QuotedStr(Result)
    else
      AstrEntidades := AstrEntidades + ', ' + QuotedStr(Result);
  end else
    Result := '';
  tq.Close;
  tq.Free;
end;

function TdmSPED.posicao(campo: string): Integer;
begin
  result := StrToIntDef(LerArquivoIni(arq, 'POSICAO', campo), 0);
end;

function TdmSPED.tamanho(campo: string): Integer;
begin
  result := StrToIntDef(LerArquivoIni(arq, 'TAMANHO', campo), 0);
end;

end.

